package org.yefei.homeworkday25;

import java.io.*;

public class Zero {

	public static void main(String[] args) {
		// 0：课堂上的模板替换那个，写完整
		InputStream fi = null;
		Reader re = null;
		BufferedReader r = null;
		Writer wr = null, w = null;
		try {
			fi = new FileInputStream("F:\\叶飞io测试\\IOhomework测试\\pet.template");
			re = new InputStreamReader(fi, "utf-8");
			r = new BufferedReader(re);
			// 按行读
			String line = null;
			StringBuilder st = new StringBuilder();
			while ((line = r.readLine()) != null) {
				st.append(line);
			}
			System.out.println("替换前的：" + st);
			// 一步替换
			String newline = st.toString().replace("{name}", "欧欧").replace("{type}", "狗狗").replace("{master}", "李伟");
			System.out.println("替换后的：" + newline);
			// 写入文件
			wr = new FileWriter("F:\\叶飞io测试\\IOhomework测试\\test.txt");
			w = new BufferedWriter(wr);
			w.write(newline);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (w != null)
				try {
					w.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			if (wr != null)
				try {
					wr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			if (r != null)
				try {
					r.close();
				} catch (IOException e) {
				}
			if (re != null)
				try {
					re.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			if (fi != null)
				try {
					fi.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}

	}

}
